package com.llkj.qingnianguoxue.mine.mycoin;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.CostRecordListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.AgreementActivity;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的积分
 */
public class MyScoreActivity extends BaseActivity {

    @BindView(R.id.tv_my_coin)
    TextView tvMyCoin;//积分
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_score);
        setTitle("范儿币", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_TEXT,"明细");
        tvRight.setTextColor(ContextCompat.getColor(this,R.color.color_black_333333));
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);

        webView = (WebView) findViewById(R.id.webView);
        WebSettings settings = webView.getSettings();
        //支持javascript
        settings.setJavaScriptEnabled(true);
        //自适应屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);

        initListener();
        loadWaitDialog();
        String url = HttpMethod.getTurntable();
        webView.loadUrl(url);
    }

    private void initListener(){
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    dismissWaitDialog();
                } else {
                    // 加载中
                }
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
                result.cancel();
                return true;
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                result.cancel();
                return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                result.cancel();
                return true;
            }
        });

    }

    @OnClick({R.id.btnRecord, R.id.btnRule, R.id.tvScan})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btnRecord://记录
                startActivity(WinningRecordActivity.class);
                break;
            case R.id.btnRule://规则
//                startActivity(WinningRuleActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("from_class", 5);
                startActivity(AgreementActivity.class, bundle);
                break;
            case R.id.tvScan://去观看
                startActivity(CoinExchangeActivity.class);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        String integral = MyApplication.getInstance().sputil.getString(ConstantUserInfo.USER_INTEGRAL);//积分
        tvMyCoin.setText("当前范儿币：" + integral);
    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        startActivity(CoinListActivity.class);
    }
}
